/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.model;
/**
 * 说明：
 * 应付款汇总表控制界面
 * @Author：黄盛强
 * @Date：2012-03-23
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TBillDetail;
import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TMoneyInOut;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.DateUtil;
import com.ruipengkj.service.TBillDetailService;
import com.ruipengkj.service.TEmployeeService;

public class StatementMoneyModel extends AbstractTableModel {
	
	private static final long serialVersionUID = 1L;
	private List<TMoneyInOut> tMoneyInOutList=new ArrayList<TMoneyInOut>();
	private Vector<String> columns = new Vector<String>();
	
	private TBillDetailService tBillDetailService;//单据商品明细Service
	private TEmployeeService tEmployeeService;
	public StatementMoneyModel(boolean b){
		tBillDetailService = BuildSpringApplication.getInstance().getTBillDetailServiceImpl();
		tEmployeeService= BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		columns.add("编号");
		if (!b) {
			columns.add("收款人");
			columns.add("收款类型");
		} else {
			columns.add("付款人");
			columns.add("付款类型");
		}
		columns.add("总额");
		columns.add("时间");
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		switch(columnIndex) {
		case 3:
			return Double.class;
		}
		return super.getColumnClass(columnIndex);
	}
	//获得行数
	public int getRowCount(){
		return tMoneyInOutList.size();
	}
	//获得列数
	public int getColumnCount(){
		return columns.size();
	}
	//获得单元格的值
	public Object getValueAt(int row,int column){
		TMoneyInOut TMoneyInOut = tMoneyInOutList.get(row);
		return getTMoneyInOutValueByCol(TMoneyInOut, column);
	}
	
	public Object getTMoneyInOutValueByCol(TMoneyInOut tMoneyInOut, int column) {
		switch (column) {
			case 0:
				return tMoneyInOut.getInOutID();
			case 1:
				if(tMoneyInOut.getTEmployee()!=null&&tMoneyInOut.getTEmployee().getName()!=null){
					return tMoneyInOut.getTEmployee().getName();
				}else{
					return "    ";
				}
			case 2:
				return ApplicationContext.MONEYINOUT_SOURCE_TYPE_MAP().get(tMoneyInOut.getSourceType());
			case 3:
				if(tMoneyInOut.getInOutID() == null || tMoneyInOut.getInOutID().equals(""))
					return null;
				return tMoneyInOut.getGross();
			case 4:
				return tMoneyInOut.getTime();
			
		}
		return null;
	}
	
	private double getMoney(byte type, String Source) {//计算总金额
		try {
			byte billType = 0;//记录类型
			switch (type) {
				case 1://采购付款
					billType = ApplicationContext.BILL_BILLTYPE1;
					break;
				case 2://2为采购退货引起收款
					billType = ApplicationContext.BILL_BILLTYPE2;
					break;
				case 3://3为销售引起的收款 
					billType = ApplicationContext.BILL_BILLTYPE3;
					break;
				case 4://4为销售退货引起的出款
					billType = ApplicationContext.BILL_BILLTYPE4;
					break;
				case 5://5为充值的出款
					billType = ApplicationContext.BILL_BILLTYPE9;
					break;
			}
			if (Source != null && !Source.equals("")) {
				TBillDetail tBillDetail = new TBillDetail();
				if (type != 5)
					tBillDetail.setBillID(Source);
				else
					tBillDetail.setDetailID(Source);
				tBillDetail.setBillType(billType);
				List<TBillDetail> billDetailList = tBillDetailService.findMoneyService(tBillDetail);
				if (billDetailList != null &&  billDetailList.size()!= 0) {
					int length = billDetailList.size();
					double money = 0;
					for (int i = 0; i < length; ++i) {
						//计算金额
						money += billDetailList.get(i).getNum() * billDetailList.get(i).getPrice();
					}
					return money;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	//获取列名
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	
	//设置单元格是否可编辑
	@Override
	public boolean isCellEditable(int rowIndex,int columnIndex){
		return false;
	}
	
	//返回 list
	public List<TMoneyInOut> getTMoneyInOutList() {
		return tMoneyInOutList;
	}
	
	//返回指定 list中的TMoneyInOut
	public TMoneyInOut getTMoneyInOut(int select) {
		return tMoneyInOutList.get(select);
	} 
	
	//设置List中的值
	public void setRowData(List<TMoneyInOut> tMoneyInOutList) {
		this.tMoneyInOutList = tMoneyInOutList;
		for (int i = tMoneyInOutList.size() - 1; i >= 0; --i) {
			TMoneyInOut tMoneyInOut = tMoneyInOutList.get(i);//计算金额
			tMoneyInOut.setGross(getMoney(tMoneyInOut.getSourceType(), tMoneyInOut.getSource()));
			//时间
			tMoneyInOut.setTime(DateUtil.format((Object)tMoneyInOut.getHappenTime(), DateUtil.TYPE_DATE));
			
			if(tMoneyInOut.getPersonID()!=null){
				try {
					TEmployee tEmployee=tEmployeeService.getNameByIdService(tMoneyInOut.getPersonID());
					tMoneyInOut.setTEmployee(tEmployee);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	//排序
	public void sort(boolean type) {
		if (tMoneyInOutList != null && tMoneyInOutList.size() != 0)
			Collections.sort(tMoneyInOutList, new ContentComparator(type));
	}
	
	//排序条件
	class ContentComparator implements Comparator<Object> {
		private boolean type;//排序类型 1按时间排序 0按付款人/收款人排序
		public ContentComparator(boolean type) {
			this.type = type;
		}
		public int compare(Object o1, Object o2) {
			TMoneyInOut tMoneyInOut1 = (TMoneyInOut)o1;
			TMoneyInOut tMoneyInOut2 = (TMoneyInOut)o2;
			if (type) {
				if (tMoneyInOut1.getTime()!=null && tMoneyInOut2.getTime()!= null)
					return tMoneyInOut1.getTime().compareTo(tMoneyInOut2.getTime());
				else
					return 0;
			} else {
				if (tMoneyInOut2.getPersonID() != null && tMoneyInOut1.getPersonID() != null)
					return tMoneyInOut1.getPersonID().compareTo(tMoneyInOut2.getPersonID());
				else {
					return 0;
				}
			}
		}
	}
	
	public void clear() {
		tMoneyInOutList.clear();
	}
}
